import cluster from 'cluster';
import * as os from 'os';
import { isDev } from '@/config';

export default function(bootstrap) {
    if (isDev) {
        bootstrap();
    } else {
        const numCPUs = os.cpus().length;
        console.log('numCPUs', numCPUs);
        if (cluster.isPrimary) {
            console.log(`Master ${process.pid} is running`);
            // Fork workers.
            for (let i = 0; i < numCPUs; i++) {
                // console.log("i", i);
                cluster.fork();
            }
            cluster.on('exit', (worker, code, signal) => {
                console.log(`Worker ${worker.process.pid} died with code: ${code}, and signal: ${signal}`);
                console.log('Starting a new worker');
                cluster.fork();
            });
        } else {
            bootstrap();
        }
    }

}